<template>
  <div class="page-wrapper">
    <c-card title="合同列表" class="com-wrapper">
      <el-button slot="func" type="primary" size="small" class="item" @click="showAddDlg">添加</el-button>
      <div class="search-row">
        <c-search
          v-model="searchText"
          :height="30"
          :width="200"
          placeholder="合同名称搜索"
          class="item"
          @submit="_getContractList"
        />
      </div>
      <el-table
        v-loading="loading"
        :data="contractList"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="conName" label="合同名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="conNum" label="合同编号"></el-table-column>
        <el-table-column prop="jointime" label="服务接入时间" width="150"></el-table-column>
        <el-table-column prop="conExeTime" label="服务执行时间" width="150"></el-table-column>
        <el-table-column prop="expirationtime" label="合同到期时间" width="150"></el-table-column>
        <el-table-column prop="totalNumber" label="合同运维总次数" width="150"></el-table-column>
        <el-table-column prop="note" label="备注" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="190">
          <template v-slot="{row}">
            <el-button size="small" type="text" @click="showEditDlg(row)">编辑</el-button>
            <el-button size="small" type="text" @click="deleteCon(row)">删除</el-button>
            <el-button size="small" type="text" @click="goto(row)">运维计划</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <div class="pagination">
        <el-pagination
          layout="prev, pager, next"
          :page-size="page.size"
          :current-page.sync="page.index"
          :total="page.total"
          @current-change="_getContractList"
        ></el-pagination>
      </div>-->
    </c-card>
    <c-dlg
      v-if="contract"
      :title="(contract.pkid ? '编辑' : '添加') + '合同'"
      :visible.sync="dialogVisible"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="contract"
        label-width="60px"
        class="demo-ruleForm"
        label-position="top"
        style="width: 100%;"
      >
        <div class="form-row">
          <div class="row-item">
            <el-form-item label="合同名称" prop="conName">
              <el-input v-model="contract.conName" size="small"></el-input>
            </el-form-item>
          </div>
          <span class="spliter"></span>
          <div class="row-item">
            <el-form-item label="合同编号" prop="conNum">
              <el-input v-model="contract.conNum" size="small"></el-input>
            </el-form-item>
          </div>
        </div>
        <div class="form-row">
          <div class="row-item">
            <el-form-item label="客户公司" prop="clientId">
              <el-select v-model="contract.clientId" size="small">
                <el-option v-for="c in comList" :key="c.pkid" :value="c.pkid" :label="c.name"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <span class="spliter"></span>
          <div class="row-item">
            <el-form-item label="合同类型" prop="conType">
              <el-select v-model="contract.conType" size="small">
                <el-option :value="1" label="预试合同"></el-option>
                <el-option :value="2" label="巡检合同"></el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
        <div class="form-row">
          <div class="row-item">
            <el-form-item label="服务接入时间" prop="jointime">
              <el-date-picker
                v-model="contract.jointime"
                size="small"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
          </div>
          <span class="spliter"></span>
          <div class="row-item">
            <el-form-item label="执行合同时间" prop="conExeTime">
              <el-date-picker
                v-model="contract.conExeTime"
                size="small"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
          </div>
        </div>
        <div class="form-row">
          <div class="row-item">
            <el-form-item label="合同到期时间" prop="expirationtime">
              <el-date-picker
                v-model="contract.expirationtime"
                size="small"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
          </div>
          <span class="spliter"></span>
          <div class="row-item">
            <el-form-item label="合同运维总次数" prop="totalNumber">
              <el-input v-model="contract.totalNumber" size="small"></el-input>
            </el-form-item>
          </div>
        </div>
        <div class="form-row">
          <div class="row-item">
            <el-form-item label="鉴定周期" prop="signingCycle">
              <el-select v-model="contract.signingCycle" size="small">
                <el-option v-for="(c, i) in cycle" :key="i" :label="c" :value="i + 1"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <span class="spliter"></span>
          <div class="row-item"></div>
        </div>
        <div class="form-row">
          <div class="row-item">
            <el-form-item label="备注" prop="clientNumber">
              <el-input v-model="contract.note" type="textarea" size="small"></el-input>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <div slot="footer">
        <el-button size="small" type="danger" @click="submitForm">提交</el-button>
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
      </div>
    </c-dlg>
  </div>
</template>

<script>
import CCard from "@/components/CCard";
import CDlg from "@/components/CDlg";
import CSearch from "@/components/CSearch";
import { getContractList, saveContract, deleteContract } from "@/api/user";
export default {
  name: "Contract",
  components: {
    CCard,
    CDlg,
    CSearch
  },
  data() {
    return {
      loading: false,
      page: {
        index: 1,
        size: 10,
        total: 0
      },
      rules: {
        conName: [
          { required: true, message: "请输入合同名称", trigger: "blur" }
        ],
        conNum: [
          { required: true, message: "请输入合同编号", trigger: "blur" }
        ],
        jointime: [
          { required: true, message: "请选择服务接入时间", trigger: "blur" }
        ],
        conExeTime: [
          { required: true, message: "请选择执行合同时间", trigger: "blur" }
        ],
        expirationtime: [
          { required: true, message: "请选择合同到期时间", trigger: "blur" }
        ],
        totalNumber: [
          { required: true, message: "请输入合同运维总次数", trigger: "blur" }
        ],
        clientId: [
          { required: true, message: "请选择客户公司", trigger: "blur" }
        ],
        conType: [
          { required: true, message: "请选择合同类型", trigger: "blur" }
        ],
        signingCycle: [
          { required: true, message: "请选择鉴定周期", trigger: "blur" }
        ]
      },
      searchText: "",
      dialogVisible: false,
      contractList: null,
      contract: null,
      // options
      cycle: ["一年一签", "两年一签", "三年一签", "四年一签", "五年一签"]
    };
  },
  computed: {
    comId() {
      return this.$store.getters.comId;
    },
    comList() {
      return this.$store.getters.comList;
    }
  },
  async mounted() {
    // const h = this.$refs["table"].$el.clientHeight;
    // const n = calcPageNum(h);
    // this.$set(this.page, "size", n);

    this.loading = true;
    try {
      this.contractList = await getContractList(this.comId);
      this.loading = false;
    } catch (e) {
      this.$message.error(e);
      this.loading = false;
    }
  },
  methods: {
    async _getContractList() {
      this.contractList = await getContractList(this.comId, this.searchText);
    },
    showEditDlg(row) {
      if (this.contract && this.contract.pkid === row.pkid) {
        this.dialogVisible = true;
      } else {
        this.contract = row;
        this.dialogVisible = true;
      }
    },
    showAddDlg() {
      if (!this.contract) {
        this.contract = {};
      }
      if (this.contract && this.contract.pkid) {
        this.contract = {};
      }
      this.dialogVisible = true;
    },
    deleteCon(row) {
      this.$confirm("此操作将永久删除该合同, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteContract(row.pkid).then(() => {
          this._getContractList();
        });
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const load = this.$loading();
          return saveContract(this.contract)
            .then(() => {
              this.dialogVisible = false;
              this.contract = null;
              this._getContractList();
            })
            .catch(e => this.$message.error(e))
            .finally(() => load.close());
        }
      });
    },
    goto(row) {
      this.$router.push({
        path: `/company/contract/${row.pkid}/maintain`,
        query: {
          name: row.conName
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
